package com.shanghaionstar.user.presenter;

import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.entity.SubscriberDTO;
import com.onstar.cn.vehicle.entity.ErrorEnum;
import com.onstar.cn.vehicle.entity.ErrorInfo;
import com.shanghaionstar.user.iview.IPayCardVisitorActivieView;
import com.shanghaionstar.user.service.PayCardVisitorActiveService;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/4.
 */
public class PayCardVisitorActivePresenter extends BasePresenter<SubscriberDTO> {
    private Context context;
    private EditText et_car_identify;

    private IPayCardVisitorActivieView payCardVisitorActivieView;
    private PayCardVisitorActiveService service;

    public PayCardVisitorActivePresenter(IMVPScheduler mvpScheduler,Context context,EditText et_car_identify) {
        super(mvpScheduler);
        this.context=context;
        this.et_car_identify=et_car_identify;
    }

    @Override
    public void setView(IMVPView<SubscriberDTO> view) {
        super.setView(view);
        payCardVisitorActivieView = (IPayCardVisitorActivieView) view;
        service = new PayCardVisitorActiveService(context, et_car_identify);
        setServiceProvider(service);
    }


    public void getData(){
        payCardVisitorActivieView.setLoading(true);
        subscription=service.getObserval().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<SubscriberDTO>() {
                    @Override
                    public void call(SubscriberDTO subscriberDTO) {
                        if(!CommonUtil.isNull(subscriberDTO)) {
                            payCardVisitorActivieView.setLoading(false);
                            payCardVisitorActivieView.showSuccess(subscriberDTO);
                        }else {
                            payCardVisitorActivieView.setLoading(false);
                            Bundle bundle=new Bundle();
                            ErrorInfo errorInfo=new ErrorInfo();
                            errorInfo.setCode(ErrorEnum.SB027_MSG007.getStringValue());
                            errorInfo.setMsg("预付卡激活失败");
                            bundle.putSerializable("errorInfo",errorInfo);
                            payCardVisitorActivieView.showFailure(bundle);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Bundle bundle=new Bundle();
                        ErrorInfo errorInfo=new ErrorInfo();
                        errorInfo.setCode(ErrorEnum.SB027_MSG007.getStringValue());
                        errorInfo.setMsg("预付卡激活失败");
                        bundle.putSerializable("errorInfo",errorInfo);
                        payCardVisitorActivieView.setLoading(false);
                        payCardVisitorActivieView.showError(throwable,bundle);
                    }
                });
    }

}
